<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Upload extends CI_Controller {
	
	var $eData = '';
	
	function __construct()
	{
		parent::__construct();
		$this->thor->loginstate();
	}
	
	function index()
	{
		$this->load->view('mang/upload', array('eType' => $this->input->get('type'), 'eID' => $this->input->get('id')));
	}

	function submit()
	{
		$eType = $this->input->get('type');
		$eID   = $this->input->get('id');
		
		$eConfig = array();
		$eConfig['upload_path'] = 'uploadfile/'.date('Y').'/'.date('W').'/';
		if($eType == 'imges') $eConfig['allowed_types'] = 'jpg|bmp|gif|png';
		else $eConfig['allowed_types'] = 'jpg|bmp|gif|png|zip|rar|7z|xls|doc|xlsx|docx|pdf|txt|ppt';
		$eConfig['file_name'] = time().rand(1000, 9999);
		$eConfig['max_size'] = 1024 * 20;
		
		$this->load->library('upload', $eConfig);
		if ( ! $this->upload->do_upload('upload'))
		{
			echo '<body style="margin: 0; padding: 0;">
			<div style="font-size: 12px; color: #CC3300;">
			'. $this->upload->display_errors() .'
			<a href="'.site_url('mang/upload').'?type='.$eType.'&id='.$eID.'">Back</a>
			</div>
			</body>';
		}
		else
		{
			$eUpLoad = $this->upload->data();
			$newsPic = $eConfig['upload_path'].$eUpLoad['file_name'];
			
			if($eType == 'imges')
			{
				echo '<script type="text/javascript">
				parent.$("#newspic").val("/'.$newsPic.'");
				parent.$("#imgPreview").attr("src", "/'.$newsPic.'");
				location.href = "'.site_url('mang/upload').'?type='.$eType.'&id='.$eID.'";
				</script>';
			}
			else if($eType == 'picList')
			{
				echo '<script type="text/javascript">
				parent.$("#picList").append("<td valign=\"top\" align=\"center\"><input name=\"pic[]\" type=\"hidden\" value=\"/'.$newsPic.'\"><img src=\"/'.$newsPic.'\" height=\"60\" /><br /><a href=\"javascript:\" onclick=\"delImg(this)\">删除</a></td>");
				location.href = "'.site_url('mang/upload').'?type='.$eType.'&id='.$eID.'";
				</script>';
			}
			else if($eType == 'list')
			{
				echo '<script type="text/javascript">
				parent.$("#picUrl'.$eID.'").val("/'.$newsPic.'");
				parent.$("#picSrc'.$eID.'").attr("src", "/'.$newsPic.'");
				location.href = "'.site_url('mang/upload').'?type='.$eType.'&id='.$eID.'";
				</script>';
			}
			else
			{
				echo '<script type="text/javascript">
				parent.$("#newsfile").val("/'.$newsPic.'");
				location.href = "'.site_url('mang/upload').'?type='.$eType.'&id='.$eID.'";
				</script>';
			}
		}
	}
}